package html;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.Iterator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class EditHtml
{
    private File fileIn;
    private FileReader fis;
    private BufferedReader bin;
    private char[] cbuf;
    private String cont;
    private Document doc;
    private Elements elements;
    private Iterator<Element> iterator;
    private Element element;
    private File fileOut;
    private FileWriter fw;
    private final String fileName;
    public EditHtml(String fName)throws Exception
    {
        fileName=fName;
        fileIn=new File(fileName);
        cbuf=new char[(int)fileIn.length()];
        fis=new FileReader(fileIn);
        fis.read(cbuf);
        cont=new String(cbuf);
    }
    public void setIfAttrib(String attrib,String ifCond,String newValue)
    {
        doc = Jsoup.parse(cont);
        elements=doc.getElementsByAttributeValueEnding(attrib, ifCond);
        iterator=elements.iterator();
        while(iterator.hasNext())
        {
            element=iterator.next();
            element.attr(attrib,newValue);
        }
    }
    public void writeToFile()throws Exception
    {
        fileOut=new File("htmlOut.html");
        fw=new FileWriter(fileOut);
        fw.write(doc.toString());
        fw.flush();
        System.out.println("done");
    }
    public static void main(String[] args)throws Exception
    {
        String fName=args[0].trim();        
        int dotLoc=fName.lastIndexOf(".");
        String extn=fName.substring(dotLoc+1, fName.length()).trim();
        System.out.println("extn: "+extn);
    }
}
